//
//  OneSelectView.m
//  susnsuns
//
//  Created by KWSD on 15/12/9.
//  Copyright © 2015年 KWSD. All rights reserved.
//

#import "OneSelectView.h"
#import "OneSelectCell.h"
#import "Masonry.h"
#import "UIColor+CL.h"
@interface OneSelectView ()<UITableViewDataSource,UITableViewDelegate>{
    CGRect rect;
    UIView *v;
}
@property(nonatomic,assign)NSInteger indexPathNum;
@end


@implementation OneSelectView

-(instancetype)initWithFrame:(CGRect)frame{
    rect = frame;
    self =[super initWithFrame:frame];
    if(self){
        
        v =[[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:v];
        self.backgroundColor = [UIColor whiteColor];
        
        
        self.layer.cornerRadius = 8;
        v.backgroundColor =[UIColor clearColor];
        v.layer.cornerRadius = 10;
        v.layer.masksToBounds = YES;
        _tb =[[UITableView alloc]init];
        _tb.frame = CGRectMake(1, 1, frame.size.width-2, frame.size.height);
        [v addSubview:_tb];
        _tb.dataSource = self;
        _tb.delegate = self;
        _tb.bounces = NO;
//        self.userInteractionEnabled = YES;
//        _tb.userInteractionEnabled = YES;
//        _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
        
//        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
////        [self addSubview:_leftBtn];
//        [_leftBtn setTitle:@"取消" forState:UIControlStateNormal];
//        [_leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        _leftBtn.frame = CGRectMake(0, frame.size.height-49+1, frame.size.width/2 - 0.5, 48);
//        _leftBtn.layer.borderColor = [[UIColor clearColor]CGColor];
//        _leftBtn.layer.borderWidth = 1;
//        _leftBtn.backgroundColor =[UIColor whiteColor];
//        _leftBtn.layer.cornerRadius = 6;
//        [_leftBtn setTitleColor:[UIColor colorWithHex:0x00a8fd] forState:UIControlStateNormal];
//        
//        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
////        [self addSubview:_rightBtn];
//        [_rightBtn setTitle:@"确定" forState:UIControlStateNormal];
//        [_rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        _rightBtn.frame = CGRectMake(frame.size.width/2 +0.5, frame.size.height-49+1, frame.size.width/2 - 0.5, 48);
//        _rightBtn.layer.borderColor = [[UIColor clearColor]CGColor];
//        _rightBtn.layer.borderWidth = 1;
//        _rightBtn.backgroundColor =[UIColor whiteColor];
//        _rightBtn.layer.cornerRadius = 6;
//        [_rightBtn setTitleColor:[UIColor colorWithHex:0x00a8fd] forState:UIControlStateNormal];
//        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, frame.size.height-49, _leftBtn.frame.size.width + _rightBtn.frame.size.width, 1)];
//        [self addSubview:imgView];
//        imgView.backgroundColor = [UIColor colorWithHex:0xd5d5d5];
//        
//        UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(_leftBtn.frame.origin.x + _leftBtn.frame.size.width, frame.size.height-49+1, 1, frame.size.height - (frame.size.height-49+1))];
//        [self addSubview:imgView1];
//        imgView1.backgroundColor = [UIColor colorWithHex:0xd5d5d5];

    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"identifier";
    OneSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if(cell==nil){
        cell = [[OneSelectCell alloc]initIdentifier:str];
    }
     NSDictionary *dic =[_dataArr objectAtIndex:indexPath.row];
    cell.la.text = [dic objectForKey:@"DIC_NAME"];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.btn.tag = indexPath.row+1000;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIButton *btn = (UIButton *)[self viewWithTag:1000+indexPath.row];
//    if(_indexPathNum>0){
//    UIButton *btn2 = (UIButton *)[self viewWithTag:_indexPathNum];
//   }
    if(btn.selected){
    [btn setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
        btn.selected = NO;
    }
    else{
         [btn setImage:[UIImage imageNamed:@"形状-20-拷贝"] forState:UIControlStateNormal];
        btn.selected =YES;
        if(_indexPathNum>0&&btn.tag!=_indexPathNum){
            UIButton *btn2 = (UIButton *)[self viewWithTag:_indexPathNum];
           [btn2 setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
            btn2.selected = NO;
        }
        _indexPathNum=btn.tag;
        NSDictionary *dic =[_dataArr objectAtIndex:indexPath.row];
        _seleTitle = [dic objectForKey:@"DIC_NAME"];
        _seleTitleNum =[NSString stringWithFormat:@"%@",[dic objectForKey:@"DIC_ID"]];
    }
    if (self.delegateView!=nil) {
        [self.delegateView performSelector:@selector(rightBtnClick:) withObject:nil];
    }
    if (self.seleSearchView!=nil) {
        [self.seleSearchView performSelector:@selector(rightBtnClick1:) withObject:nil];
    }

}


-(void)setDataArr:(NSArray *)dataArr{
    NSLog(@"~~~%ld",(long)_indexPathNum);
    if(_indexPathNum>0){
         UIButton *btn = (UIButton *)[self viewWithTag:_indexPathNum];
        [btn setImage:[UIImage imageNamed:@"椭圆-1-拷贝-3"] forState:UIControlStateNormal];
        btn.selected = NO;
    }
    _dataArr = dataArr;
    [_tb reloadData];
    if(dataArr.count<4){
        
        CGRect rect1     = rect;
        rect1.size.height = dataArr.count*40;
        v.frame = CGRectMake(0, 0, rect1.size.width, rect1.size.height);
        _tb.frame = CGRectMake(1, 1, rect1.size.width-2, rect1.size.height);
        
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, rect1.size.height+5);
    }else{
        v.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        _tb.frame = CGRectMake(1, 1, rect.size.width-2, rect.size.height);
        
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, rect.size.height+5);
    }
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 20)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}


@end
